package com.senriot.quickup.service.mapper

import com.senriot.cloud.common.IEntityMapper
import com.senriot.quickup.domain.Bonus
import com.senriot.quickup.domain.Device
import com.senriot.quickup.domain.User
import com.senriot.quickup.service.dto.BonusDTO
import com.senriot.quickup.service.dto.DeviceDTO
import org.mapstruct.*

/**
 * Mapper for the entity [Device] and its DTO [DeviceDTO].
 */
@Mapper(
        componentModel = "spring",
        uses = [StoreMapper::class, ProductMapper::class, CityMapper::class],
        nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE
)
abstract class DeviceMapper : IEntityMapper<DeviceDTO, Device>
{

    @Mappings(
            Mapping(target = "deviceName", source = "name"),
            Mapping(target = "store", source = "storeId"),
            Mapping(target = "product", source = "productId"),
            Mapping(target = "city", source = "cityId"),
            Mapping(target = "province", source = "provinceId"),
            Mapping(target = "district", source = "districtId")
    )
    abstract override fun toEntity(dto: DeviceDTO): Device

    @Mappings(
            Mapping(target = "name", source = "deviceName"),
            Mapping(target = "storeId", source = "store.id"),
            Mapping(target = "storeName", source = "store.name"),
            Mapping(target = "productId", source = "product.id"),
            Mapping(target = "productName", source = "product.name"),
            Mapping(target = "cityId", source = "city.id"),
            Mapping(target = "provinceId", source = "province.id"),
            Mapping(target = "districtId", source = "district.id"),
            Mapping(target = "cityName", source = "city.name"),
            Mapping(target = "provinceName", source = "province.name"),
            Mapping(target = "districtName", source = "district.name")
    )
    abstract override fun toDto(entity: Device): DeviceDTO


    @Mappings(
            Mapping(target = "id", ignore = true),
            Mapping(target = "deviceName", source = "name"),
            Mapping(target = "store", source = "storeId"),
            Mapping(target = "product", source = "productId"),
            Mapping(target = "createdBy", ignore = true),
            Mapping(target = "createdDate", ignore = true),
            Mapping(target = "lastModifiedBy", ignore = true),
            Mapping(target = "lastModifiedDate", ignore = true),
            Mapping(target = "city", source = "cityId"),
            Mapping(target = "province", source = "provinceId"),
            Mapping(target = "district", source = "districtId")
    )
    abstract override fun updateEntity(dto: DeviceDTO, @MappingTarget entity: Device): Device


    @Mappings(
            Mapping(target = "deviceId", source = "device.id"),
            Mapping(target = "userId", source = "user.id"),
            Mapping(target = "login", source = "user.login"),
            Mapping(target = "userName", source = "user.userName"),
            Mapping(target = "imageUrl", source = "user.imageUrl"),
            Mapping(target = "deviceName", source = "device.deviceName")
    )
    abstract fun bonusToDto(entity: Bonus): BonusDTO

    @Mappings(
            Mapping(target = "device", source = "deviceId"),
            Mapping(target = "user", source = "userId")
    )
    abstract fun bonusToEntity(dto: BonusDTO): Bonus

    abstract fun bonusToEntities(dto: List<BonusDTO>): MutableList<Bonus>

    abstract fun bonusToDto(entity: List<Bonus>?): MutableList<BonusDTO>?

    fun fromId(id: String?) = id?.let { Device(id = it) }

    fun getId(device: Device) = device.id

    fun userFromId(id: Long) = User(id = id)


}
